package com.fernfx.xingtan.user.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.verifycode.VerifyCodeSend;
import com.fernfx.xingtan.user.contract.RegisterContract;
import com.fernfx.xingtan.user.presenter.RegisterPresenter;
import com.fernfx.xingtan.utils.MobileInfoUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.RegexUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/1.
 * 注册用户
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener, RegisterContract.View {
    @BindView(R.id.user_account_edit)
    EditText userAccountEdit;

    @BindView(R.id.register_clt)
    ViewGroup registerClt;

    @BindView(R.id.send_verify_code_tv)
    TextView sendVerifyCodeTv;

    @BindView(R.id.verify_code_edit)
    EditText verifyCodeEdit;

    @BindView(R.id.input_pwd_edit)
    EditText inputPwdEdit;

    private VerifyCodeSend verifyCodeSend = new VerifyCodeSend(this,"register");

    //init Presenter
    private RegisterContract.Presenter presenter = new RegisterPresenter();

    private int eyeWidth;

    public static void start(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_user_register;
    }

    @Override
    protected void initView() {
        setToolbarTitle(getString(R.string.fast_register));
    }

    @Override
    protected void initData() {
        presenter.init(this);
    }

    @Override
    protected void initListener() {
    }

    @OnClick({R.id.register_clt, R.id.send_verify_code_tv, R.id.confirm_register_bt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_clt:
                SoftInputUtil.hideSoftInput(mContext, registerClt);
                break;
            case R.id.send_verify_code_tv:
                //发送验证码
                boolean flag = verifyCodeSend.send(trimEditText(userAccountEdit));
                if (flag) {
                    CountDownTimer countDownTimer = presenter.getTimeCount(sendVerifyCodeTv);
                    countDownTimer.start();
                    //验证码获得焦点
                    OtherUtil.editTextRequestFocus(verifyCodeEdit);
                }
                break;
            case R.id.confirm_register_bt:
                //验证手机号
                String userAccountText = trimEditText(userAccountEdit);
                if (!RegexUtil.isMobileNumber(userAccountText)) {
                    OtherUtil.editTextRequestFocus(userAccountEdit);
                    ToastUtil.showCentertoast(R.string.input_proper_pnumber);
                    return;
                }
                String verifyCodeText = trimEditText(verifyCodeEdit);
                if (verifyCodeText.length() != 6) {
                    OtherUtil.editTextRequestFocus(verifyCodeEdit);
                    ToastUtil.showCentertoast(R.string.input_proper_vcode);
                    return;
                }
                String inputPwdText = trimEditText(inputPwdEdit);
                if (!RegexUtil.isPwd(inputPwdText)) {
                    OtherUtil.editTextRequestFocus(inputPwdEdit);
                    ToastUtil.showCentertoast(R.string.pwd_error);
                    return;
                }
                //init request map
                requestArgsMap.clear();
                requestArgsMap.put("areaCode", Constant.RequestArgs.AREA_CODE);
                requestArgsMap.put("imei", MobileInfoUtil.getIMEI(this));
                requestArgsMap.put("os", Constant.RequestArgs.SYSTEM_TYPE);

                requestArgsMap.put("smsCode", verifyCodeText);
                requestArgsMap.put("mobile", userAccountText);
                requestArgsMap.put("password", inputPwdText);
                presenter.request(requestArgsMap);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
        verifyCodeSend = null;
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }
}
